﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class MotionReviewerCommentBLL : IDisposable
    {
        DatabaseContext _DatabaseContext;
        public MotionReviewerCommentBLL()
            : this(new DatabaseContext())
        {
        }

        public MotionReviewerCommentBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public MotionReviewerComments CreateMotionReviewerComment(MotionReviewerComments motionReviewerComment)
        {
            motionReviewerComment = _DatabaseContext.MotionReviewerComments.Add(motionReviewerComment);
            _DatabaseContext.SaveChanges();
            return motionReviewerComment;
        }

        public MotionReviewerComments GetMotionReviewerCommentByMotionIDAndReviewerID(long motionID, long reviewerID, string roleType)
        {
            return _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID && m.ReviewerID == reviewerID && m.RoleType.Equals(roleType)).FirstOrDefault();                 
        }

        public List<MotionReviewerComments> GetMotionReviewerCommentByMotionID(long motionID)
        {
            return _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID).OrderByDescending(m => m.CommentDateTime).ToList();
        }

        public void Save()
        {
            _DatabaseContext.SaveChanges();
        }

        public bool AllGiveFeedback(long motionID, string roleType)
        {
            return _DatabaseContext.MotionReviewerComments.Count(m => m.MotionID == motionID && m.RoleType.Equals(roleType) && !m.CommentDateTime.HasValue) == 0;
        }
    }
}